package com.wlsk.license.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.wlsk.license.common.result.Result;
import com.wlsk.license.dto.request.AuthenticationReqDto;
import com.wlsk.license.dto.request.UserReqDto;
import com.wlsk.license.dto.request.feign.MerchantReqDto;
import com.wlsk.license.dto.request.feign.OrgReqDto;
import com.wlsk.license.dto.response.AuthenticationResultDto;
import com.wlsk.license.dto.response.SignEnterpriseInfo;
import com.wlsk.license.dto.response.feign.MerchantResultDto;
import com.wlsk.license.dto.response.feign.OrgResVo;
import com.wlsk.license.dto.response.feign.UserResDto;
import com.wlsk.license.service.IAuthenticationService;
import com.wlsk.license.service.feign.MerchantFeignClient;
import com.wlsk.license.service.feign.UserCenterClient;
import com.ykx.userCenter.provider.bean.EnterpriseInfoDto;
import com.ykx.userCenter.provider.result.EnterpriseInfoResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class AuthenticationServiceImpl implements IAuthenticationService {

    @Autowired
    private UserCenterClient userCenterClient;

    @Autowired
    private MerchantFeignClient merchantFeignClient;

    @Override
    public AuthenticationResultDto countTodo(AuthenticationReqDto reqDto) {
        AuthenticationResultDto resultDto = new AuthenticationResultDto();
        Long uid = reqDto.getUid();
        Result<UserResDto> userResDtoResult = merchantFeignClient.getUserByUserId(UserReqDto.builder().uid(uid).build());
        if(userResDtoResult.isSuccess() && userResDtoResult.getData() != null){
            Result<List> merchantResult = merchantFeignClient.getAllMerchantInfo(MerchantReqDto.builder().merchantCode(userResDtoResult.getData().getOrgVo().getMerchantCode()).build());
            if(merchantResult.isSuccess() && CollectionUtils.isNotEmpty(merchantResult.getData())){
                MerchantResultDto dto = JSONObject.parseObject(JSONObject.toJSONString(merchantResult.getData().get(0)), MerchantResultDto.class);
                resultDto.setBindAccount(StringUtils.isNotEmpty(dto.getCooperateCode()));
                resultDto.setBusinessEnabled(CollectionUtils.isNotEmpty(dto.getCarCharterConfigs()));
                try {
                    EnterpriseInfoResult enterpriseInfoResult = userCenterClient.queryEnterpriseInfoByCreditCode(dto.getCreditCode());
                    if(enterpriseInfoResult.isSuccess() && enterpriseInfoResult.getData() != null) {
                        EnterpriseInfoDto enterpriseInfo =  JSONObject.parseObject(JSONObject.toJSONString(enterpriseInfoResult.getData()), EnterpriseInfoDto.class);
                        if(enterpriseInfo != null && CollectionUtils.isNotEmpty(enterpriseInfo.getSealList())) {
                            resultDto.setMissSeal(true);
                        }
                    }
                } catch (Exception e) {
                    log.error("查询企业印章信息失败:{}", dto.getCooperateCode(), e);
                }
            }
        }
        return resultDto;
    }

    @Override
    public String findOrgOperatorCode(Long orgId, int type) {
        String orgOperatorCode = null;
        Result<OrgResVo> userResDtoResult = merchantFeignClient.orgInfo(OrgReqDto.builder().orgId(orgId).build());
        if(userResDtoResult.isSuccess() && userResDtoResult.getData() != null){
            Result<List> merchantResult = merchantFeignClient.getAllMerchantInfo(MerchantReqDto.builder().merchantCode(userResDtoResult.getData().getMerchantCode()).build());
            if(merchantResult.isSuccess() && CollectionUtils.isNotEmpty(merchantResult.getData())){
                MerchantResultDto dto = JSONObject.parseObject(JSONObject.toJSONString(merchantResult.getData().get(0)), MerchantResultDto.class);
                if(StringUtils.isNotEmpty(dto.getCooperateCode())) {
                    orgOperatorCode = type == 1 ? String.format("qf%s",  dto.getCooperateCode()) : dto.getCooperateCode();
                }
            }
        }
        return orgOperatorCode;
    }

    @Override
    public SignEnterpriseInfo findEnterpriseInfo(Long orgId) {
        SignEnterpriseInfo resultDto = new SignEnterpriseInfo();
        Result<OrgResVo> userResDtoResult = merchantFeignClient.orgInfo(OrgReqDto.builder().orgId(orgId).build());
        if(userResDtoResult.isSuccess() && userResDtoResult.getData() != null) {
            EnterpriseInfoResult enterpriseInfoResult = userCenterClient.queryEnterpriseInfoByCreditCode(userResDtoResult.getData().getMerchantCode());
            if(enterpriseInfoResult.isSuccess() && enterpriseInfoResult.getData() != null) {
                EnterpriseInfoDto enterpriseInfo =  JSONObject.parseObject(JSONObject.toJSONString(enterpriseInfoResult.getData()), EnterpriseInfoDto.class);
                if(enterpriseInfo != null && CollectionUtils.isNotEmpty(enterpriseInfo.getSealList())) {
                    resultDto.setAccountId(enterpriseInfo.getAccountId());
                    resultDto.setSealData(enterpriseInfo.getSealList().get(0).getEnterSeal());
                }
            }
        }
        return resultDto;
    }
}
